document.addEventListener('DOMContentLoaded', () => {
    const registerForm = document.getElementById('registerForm');
    const loginForm = document.getElementById('loginForm');
    const loginMessage = document.getElementById('loginMessage');
    const loginDiv = document.getElementById('login');
    const registerDiv = document.getElementById('register');

    // 正则表达式  
    const realNameRegex = /^[\u4e00-\u9fa5]{2,10}$/;
    const passwordRegex = /^[a-zA-Z][a-zA-Z0-9_]{5,14}$/;
    const mobileRegex = /^1[3-9]\d{9}$/; // 简单的手机号码验证  
    const telRegex = /^(0\d{2,3})-?(\d{7,8})$/; // 固定电话验证  

    // 验证函数  
    function validateRegisterForm() {
        let isValid = true;

        // 验证真实姓名  
        const realName = document.getElementById('realName').value;
        const realNameError = document.getElementById('realNameError');
        if (!realNameRegex.test(realName)) {
            realNameError.textContent = '请使用汉字，长度为2-10个';
            isValid = false;
        } else {
            realNameError.textContent = '';
        }

        // 验证密码  
        const password = document.getElementById('password').value;
        const passwordError = document.getElementById('passwordError');
        if (!passwordRegex.test(password)) {
            passwordError.textContent = '以字母开头，长度在6-15之间，只能包含字母、数字、下划线';
            isValid = false;
        } else {
            passwordError.textContent = '';
        }

        const confirmPassword = document.getElementById('confirmPassword').value;
        const confirmPasswordError = document.getElementById('confirmPasswordError');
        if (password !== confirmPassword) {
            confirmPasswordError.textContent = '密码不一致';
            isValid = false;
        } else {
            confirmPasswordError.textContent = '';
        }

        const mobile = document.getElementById('mobile').value;
        const mobileError = document.getElementById('mobileError');
        if (!mobileRegex.test(mobile)) {
            mobileError.textContent = '手机号码格式不正确';
            isValid = false;
        } else {
            mobileError.textContent = '';
        }

        const tel = document.getElementById('tel').value;
        const telError = document.getElementById('telError');
        if (!telRegex.test(tel)) {
            telError.textContent = '固定电话格式不正确 (如：0511-4405222、021-87888822)';
            isValid = false;
        } else {
            telError.textContent = '';
        }

        return isValid;
    }

    // 注册功能  
    registerForm.addEventListener('submit', (event) => {
        event.preventDefault();
        if (validateRegisterForm()) {
            alert('注册成功');
            setTimeout(() => {
                window.location.href = '#login'; // 使用hash跳转，实际项目中应使用更合适的方式  
                showLoginPage();
            }, 5000);
        }
    });

    // 显示登录页面  
    function showLoginPage() {
        registerDiv.style.display = 'none';
        loginDiv.style.display = 'block';
    }

    // 登录功能  
    loginForm.addEventListener('submit', (event) => {
        event.preventDefault();
        const loginName = document.getElementById('loginName').value;
        const loginPassword = document.getElementById('loginPassword').value;

        // 从本地存储中获取注册信息（实际项目中应从服务器验证）  
        const registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || [];
        const user = registeredUsers.find(u => u.name === loginName && u.password === loginPassword);
        if (user) {
            loginMessage.textContent = '登录成功';
            loginMessage.style.color = 'green';
        } else {
            loginMessage.textContent = '输入的用户名或密码不正确';
            loginMessage.style.color = 'red';
        }
    });

    // 模拟注册用户（仅用于演示）  
    document.addEventListener('DOMContentLoaded', () => {
        const registeredUsers = JSON.parse(localStorage.getItem('registeredUsers')) || [];
        if (registeredUsers.length === 0) {
            // 自动注册一个测试用户（仅在第一次加载页面时）  
            const testUser = { name: '张三', password: '123456_a' };
            registeredUsers.push(testUser);
            localStorage.setItem('registeredUsers', JSON.stringify(registeredUsers));
        }
    });
});